package first.endtoend.parse

import first.endtoend.Address
import first.endtoend.Beneficiary
import first.endtoend.Category
import first.endtoend.FairPricesShop
import first.endtoend.Family
import first.endtoend.Locality
import first.endtoend.*
import grails.validation.ValidationException

/*
@author : A.Olfa
 */

class ParseCSVBeneficiaryService
{

    Beneficiary beneficiaryInstance
    Locality localityInstance
    Address addressInstance
    Family familyInstance
    FairPricesShop shopInstance

    def parseBeneficiaries(String filepath, boolean header)
    {
        def benefs = new ArrayList<Family>()

        new File(filepath).eachCsvLine { tokens ->

            //define a default family  and a default Category

            Category categoryInstance = Category.findByName(tokens[0])


            if (categoryInstance)
            {

                //create an adress like specified in csv file, see standard related to the structure of this file
                String adresss = (String) tokens[3]
                println(adresss.size())

                addressInstance = new Address(adresss)
                if (!addressInstance.validate())
                {
                    throw new ValidationException("addressInstance is not valid", addressInstance.errors)
                }

                shopInstance = FairPricesShop.findByFpsName(tokens[7])


                Family aFamily = Family.findByFamilyNameAndTelephone((String) tokens[2], (String) tokens[4])
                if (benefs.size() == 0)
                {
                    familyInstance = new Family(familyName: tokens[2], category: categoryInstance, address: addressInstance, telephone: tokens[4])

                } else
                {
                    benefs.each {
                        if (it.familyName.equals(tokens[2]) && it.telephone.equals(tokens[4]))
                        {
                            familyInstance = it
                        } else
                        {
                            familyInstance = new Family(familyName: tokens[2], category: categoryInstance, address: addressInstance, telephone: tokens[4])
                        }
                    }
                }
                if (aFamily)
                {
                    println("family will be updated")
                    aFamily.setCategory(categoryInstance)
                    aFamily.setAddress(addressInstance)
                    if (shopInstance)
                    {
                        aFamily.setFps(shopInstance)
                    } else
                    {
                        println("this shop does not exist")
                    }

                    Beneficiary existantBenef = Beneficiary.findByFamilyAndFirstNameAndLastName(aFamily, tokens[1], tokens[2])
                    if (existantBenef)
                    {
                        existantBenef.setPhotoURL(tokens[5])
                        existantBenef.setTataId(tokens[6])
                        existantBenef.save()
                    } else
                    {
                        Beneficiary ben = new Beneficiary()
                        ben.setFirstName(tokens[1])
                        ben.setLastName(tokens[2])
                        ben.setFamily(aFamily)
                        ben.setPhotoURL(tokens[5])
                        ben.setTataId(tokens[6])
                        ben.save()
                        aFamily.beneficiaries.add(ben)
                    }
                    aFamily.save()

                } else
                {
                    if (!familyInstance.validate())
                    {
                        throw new ValidationException("Family is not valid", familyInstance.errors)
                    } else
                    {
                        //nothing
                    }
                    //familyInstance.save()

                    Beneficiary ben1 = new Beneficiary()
                    ben1.setFirstName(tokens[1])
                    ben1.setLastName(tokens[2])
                    ben1.setFamily(familyInstance)
                    ben1.setPhotoURL(tokens[5])
                    ben1.setTataId(tokens[6])
                    if (familyInstance.beneficiaries == null)
                    {
                        familyInstance.beneficiaries = new ArrayList<Beneficiary>()
                    }
                    familyInstance.beneficiaries.add(ben1)
                    if (shopInstance)
                    {
                        familyInstance.setFps(shopInstance)
                    } else
                    {
                        println("this shop does not exist")
                    }
                    if (!ben1.validate())
                    {
                        throw new ValidationException("Beneficiary is not valid", ben1.errors)
                    }


                    if (ben1.hasErrors())
                        println ben1.errors
                }
                benefs.add(familyInstance)

            } else
            {
                println("Category doesn't exist")
            }
        }
        benefs
        println("####################### SORTIT PARSE Beneficiary #######################")
    }


}
